var imagesSelected = [];

/**
 * 
 */
$(document).ready(
	function ()
	{
		$('#tree .toggle').live('click', 
		    function () 
		    { 
		    	var selector = $(this).parent();
		    	
		    	if ( $(selector).hasClass('closed') ) 
		    		$(selector).find('ul').remove();
		    } 
		);
                    		
		$('#tree .folder').click(
			function ()
			{
				$(this).parent().find('.toggle').trigger('click');
			}
		);
	}
);

/**
 * 
 * @param selector
 * @param reg_id
 */
function loadTree( selector, reg_id )
{	
	$.ajax({
		type: 'POST',
		data: {reg_id: reg_id},
		dataType: 'json',
		url: baseUrl + '/admin/galeria/local/',
		beforeSend: function()
		{
			var ul 		= $('<ul>');
			var li 		= $('<li>');
			var span 	= $('<span class="loading">').text('Carregando...');
			
			li.append( span );
			ul.append( li );
			
			$(selector).parent().append( ul );
		},
		success: function ( response )
		{
			$(selector).parent().find('ul').remove();
			
			var ul = $('<ul>');
			
			if ( response.length ) {
				
				for ( i in response ) {
					
					var li 		= $('<li>');
					var a 		= $('<a class="document" href="#">');
					var span 	= $('<span>').text( response[i].lcl_titulo );
					
					addEventElement( a, response[i] );
					
					a.append( span );
					li.append( a );
					ul.append( li );
					
				}
					
			} else {
				
				var li 		= $('<li>');
				var span 	= $('<span class="empty">').text('Vazio');
				
				li.append( span );
				ul.append( li );
				
			}
			
			$(selector).parent().append( ul );
		},
		error: function( response )
		{
		}
	});
}

/**
 * 
 * @param elem
 * @param data
 */
function addEventElement( elem, data )
{	
	var status = $('.black-cell').eq(0).find('span');
	
	elem.bind(
		'click',
		function()
		{
			$('.black-cell').eq(1).html( data.lcl_titulo ).show();
			
			imagesSelected = [];
			
			$.ajax({
				url: baseUrl + '/admin/galeria/imagem/',
				data: {lcl_id: data.lcl_id},
				dataType: 'text',
				type: 'POST',
				beforeSend: function() 
				{
					status.removeClass().addClass('loading');
				},
				success: function ( response )
				{
					$('#gallery-image').html( response );
					
					$('#gallery-image ul li a').live(
						'click',
						function ( node )
						{
							if ( !$(this).hasClass('selected') ) {
								
								$(this).addClass('selected');
								
								imagesSelected.push( $(this).attr('rel') );
								
							} else {
								
								$(this).removeClass('selected');
								
								var index = $.inArray( $(this).attr('rel'), imagesSelected );
								
								if ( -1 != index )
									imagesSelected.splice(index, 1);
								
							}
								
						}
					);
					
					status.removeClass().addClass('success');
				},
				error: function( response ) 
				{
					status.removeClass().addClass('error');
				}
			});
		}
	);
}

function removeImages()
{
	if ( imagesSelected.length && confirm('Deseja realmente remover as imagens selecionadas?') ) {
		
		var status = $('.black-cell').eq(0).find('span');
		
		$.ajax({
			url: baseUrl + '/admin/galeria/remove/',
			data: {id: imagesSelected},
			dataType: 'text',
			type: 'POST',
			beforeSend: function() 
			{
				status.removeClass().addClass('loading');
			},
			success: function ( response )
			{
				if ( response ) {
					$('#gallery-image ul li a').each(
						function ( node )
						{
							var index = $.inArray( $(this).attr('rel'), imagesSelected )
							
							if ( -1 != index ) {
								$(this).parent().remove();
								
								imagesSelected.splice( index, 1 );
							}
						}
					);
					
					status.removeClass().addClass('success');
				}
			},
			error: function( response ) 
			{
				status.removeClass().addClass('error');
			}
		});
		
	}
}